﻿@using viBook.Common
@using viBook.Models
@model IEnumerable<Activity>

@{
    var totalActivitiesPages = (int)Math.Ceiling((double)Model.Count() / Constant.MaximumActivitiesToPaging);
    totalActivitiesPages = totalActivitiesPages == 0 ? 1 : totalActivitiesPages;
    bool isShowNextButton = totalActivitiesPages > 1;
    var initionList = Model.Take(Constant.MaximumActivitiesToPaging).ToList();
}
 
<script id="activity-template" type="text/html" >
    <li data-bind="attr: {class: classBinding}">       
        <div data-bind="html: description"></div>
        <div><em class="time" data-bind="text: createdDateString"></em></div>
    </li>
</script>

<script type="text/javascript">
    function Activity(id, userId, description, createdDateString, type, referId, classBinding) {
        return {
            id: ko.observable(id),
            userId: ko.observable(userId),
            description: ko.observable($("<div/>").html(description).text()),
            createdDateString: ko.observable(createdDateString),
            type: type,
            referId: referId,
            classBinding: classBinding
        };
    }
    var initionActivitiesCollection = @CustomHelpers.ActivitiesToJavaScriptConverter(initionList),
        viewmodel = {
            totalActivitiesPages: ko.observable( @totalActivitiesPages ),
            isShowPrevBtn: ko.observable(false),
            isShowNextBtn: ko.observable( @isShowNextButton.ToString().ToLower() ),
            numberOfActivities: ko.observable( @Model.Count() ),
            activitiesPageNo : ko.observable(1),
            activitiesCollection: ko.observableArray(initionActivitiesCollection),
            fetchNextActivitiesPage: function() {
                $.ajax({
                        url: '@Url.Action("FetchNextActivitiesPage", "Account")',
                        type: 'POST',
                        data: ko.toJSON({ userId: @Model.FirstOrDefault().UserId, pageNumber: viewmodel.activitiesPageNo() + 1 }),
                        contentType: 'APPLICATION/JSON',
                        success: function(result) {
                            if (result.isSuccess) {
                                var listActivities = [];
                                $(result.activities).each(function(index, item) {
                                    if((index+1)%3!=0)
                                    listActivities[index] = new Activity(item.id,item.userId, $("<div/>").text(item.description).html(), item.createdDateString, item.type, item.referId, 'group') ;
                                    else {
                                        listActivities[index] = new Activity(item.id,item.userId, $("<div/>").text(item.description).html(), item.createdDateString, item.type, item.referId,'group third') ;
                                    }  
                                });
                                viewmodel.activitiesCollection(listActivities);
                                viewmodel.activitiesPageNo(viewmodel.activitiesPageNo() + 1);
                                viewmodel.isShowPrevBtn(viewmodel.activitiesPageNo() > 1);
                                viewmodel.isShowNextBtn(viewmodel.activitiesPageNo() < viewmodel.totalActivitiesPages());
                            } else {
                                alert(result.message);
                            }
                        }
                    });
        },
        fetchPreviousActivitiesPage: function() {
            $.ajax({
                    url: '@Url.Action("FetchNextActivitiesPage", "Account")',
                    type: 'POST',
                    data: ko.toJSON({ userId: @Model.FirstOrDefault().UserId, pageNumber: viewmodel.activitiesPageNo() - 1 }),
                    contentType: 'APPLICATION/JSON',
                    success: function(result) {
                        if (result.isSuccess) {
                            var listActivities = [];
                            $(result.activities).each(function(index, item) {
                                  if((index+1)%3!=0)
                                listActivities[index] = new Activity(item.id,item.userId, $("<div/>").text(item.description).html(), item.createdDateString, item.type, item.referId, 'group') ;
                                else {
                                    listActivities[index] = new Activity(item.id,item.userId, $("<div/>").text(item.description).html(), item.createdDateString, item.type, item.referId,'group third') ;
                                }  
                            });
                            viewmodel.activitiesCollection(listActivities);
                            viewmodel.activitiesPageNo(viewmodel.activitiesPageNo() - 1);
                            viewmodel.isShowPrevBtn(viewmodel.activitiesPageNo() > 1);
                            viewmodel.isShowNextBtn(viewmodel.activitiesPageNo() < viewmodel.totalActivitiesPages());
                        } else {
                            alert(result.message);
                        }
                    }
                });
        }
        };
    ko.applyBindings(viewmodel);
</script>